<html>
<head><meta charset="utf-8"><title>FindUsages help with local pattern · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/FindUsages.20help.20with.20local.20pattern.html">FindUsages help with local pattern</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="215989208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/FindUsages%20help%20with%20local%20pattern/near/215989208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Arif Roktim <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/FindUsages.20help.20with.20local.20pattern.html#215989208">(Nov 08 2020 at 04:52)</a>:</h4>
<p>Hello, I need some help finding usages of a local binding.<br>
Given the following fixture:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">enum</span> <span class="nc">A</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">One</span><span class="p">(</span><span class="nb">String</span><span class="p">,</span><span class="w"> </span><span class="kt">usize</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">Two</span><span class="p">(</span><span class="nb">String</span><span class="p">),</span><span class="o">&lt;|&gt;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">func</span><span class="p">(</span><span class="n">it</span>: <span class="nc">A</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">A</span>::<span class="n">One</span><span class="p">(</span><span class="n">text</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">it</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// repeat the text n times</span>
<span class="w">        </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="n">text</span><span class="p">.</span><span class="n">repeat</span><span class="p">(</span><span class="n">n</span><span class="p">));</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>I have an <code>ast::TupleStructPat</code>: <code>A::One(text, n)</code>. From this, I want to find usages for <code>text</code> and <code>n</code> . Here's the code I have so far:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">update_usage</span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="n">ctx</span>: <span class="kp">&amp;</span><span class="nc">AssistContext</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">reference</span>: <span class="kp">&amp;</span><span class="nc">Reference</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">source_file</span>: <span class="kp">&amp;</span><span class="nc">SourceFile</span><span class="p">,</span><span class="w"></span>
<span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Option</span><span class="o">&lt;</span><span class="p">()</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">pat</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">algo</span>::<span class="n">find_node_at_offset</span>::<span class="o">&lt;</span><span class="n">ast</span>::<span class="n">TupleStructPat</span><span class="o">&gt;</span><span class="p">(</span><span class="w"></span>
<span class="w">        </span><span class="n">source_file</span><span class="p">.</span><span class="n">syntax</span><span class="p">(),</span><span class="w"></span>
<span class="w">        </span><span class="n">reference</span><span class="p">.</span><span class="n">file_range</span><span class="p">.</span><span class="n">range</span><span class="p">.</span><span class="n">start</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">dbg</span><span class="o">!</span><span class="p">(</span><span class="o">&amp;</span><span class="n">pat</span><span class="p">,</span><span class="w"> </span><span class="n">pat</span><span class="p">.</span><span class="n">syntax</span><span class="p">().</span><span class="n">text</span><span class="p">());</span><span class="w"> </span><span class="cm">/* [crates/...] pat.syntax().text() = "A::One(text, n)" */</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">fields</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pat</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">fields</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">filter_map</span><span class="p">(</span><span class="o">|</span><span class="n">field</span><span class="o">|</span><span class="w"> </span><span class="k">match</span><span class="w"> </span><span class="n">field</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">ast</span>::<span class="n">Pat</span>::<span class="n">IdentPat</span><span class="p">(</span><span class="n">ident</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">ident</span><span class="p">),</span><span class="w"></span>
<span class="w">            </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="nb">None</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">});</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="n">field</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">fields</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">dbg</span><span class="o">!</span><span class="p">(</span><span class="o">&amp;</span><span class="n">field</span><span class="p">);</span><span class="w"> </span><span class="cm">/* See bottom for output */</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">usages</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Definition</span>::<span class="n">Local</span><span class="p">(</span><span class="n">ctx</span><span class="p">.</span><span class="n">sema</span><span class="p">.</span><span class="n">to_def</span><span class="p">(</span><span class="o">&amp;</span><span class="n">field</span><span class="p">)</span><span class="o">?</span><span class="p">).</span><span class="n">usages</span><span class="p">(</span><span class="o">&amp;</span><span class="n">ctx</span><span class="p">.</span><span class="n">sema</span><span class="p">).</span><span class="n">all</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="n">dbg</span><span class="o">!</span><span class="p">(</span><span class="n">usages</span><span class="p">);</span><span class="w"> </span><span class="cm">/* [crates/...] usages = [] */</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="nb">Some</span><span class="p">(())</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cm">/*</span>
<span class="cm">[crates/...] &amp;field = IdentPat {</span>
<span class="cm">    syntax: IDENT_PAT@88..92</span>
<span class="cm">      NAME@88..92</span>
<span class="cm">        IDENT@88..92 "text"</span>
<span class="cm">    ,</span>
<span class="cm">}</span>
<span class="cm">*/</span><span class="w"></span>
</code></pre></div>
<p>But the usages come out empty.</p>
<p>Am I doing something wrong here? Or is FindUsage not able to do something like this?</p>



<a name="216043467"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/FindUsages%20help%20with%20local%20pattern/near/216043467" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Arif Roktim <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/FindUsages.20help.20with.20local.20pattern.html#216043467">(Nov 09 2020 at 04:39)</a>:</h4>
<p>It might be a bug. I replaced the macro <code>println!("{}", text.repeat(n)</code> with <code>let foo = text.repeat(n)</code> and I got the expected behavior</p>



<a name="216060843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/FindUsages%20help%20with%20local%20pattern/near/216060843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/FindUsages.20help.20with.20local.20pattern.html#216060843">(Nov 09 2020 at 09:38)</a>:</h4>
<p><a href="https://github.com/rust-analyzer/rust-analyzer/blob/945900bdbf5423fdfb8542901dfb813c2ec32e09/crates/assists/src/handlers/inline_local_variable.rs">https://github.com/rust-analyzer/rust-analyzer/blob/945900bdbf5423fdfb8542901dfb813c2ec32e09/crates/assists/src/handlers/inline_local_variable.rs</a> does something similar</p>



<a name="216060872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/FindUsages%20help%20with%20local%20pattern/near/216060872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/FindUsages.20help.20with.20local.20pattern.html#216060872">(Nov 09 2020 at 09:39)</a>:</h4>
<p>Specifically, this bit I belive: <a href="https://github.com/rust-analyzer/rust-analyzer/blob/945900bdbf5423fdfb8542901dfb813c2ec32e09/crates/assists/src/handlers/inline_local_variable.rs#L45-L51">https://github.com/rust-analyzer/rust-analyzer/blob/945900bdbf5423fdfb8542901dfb813c2ec32e09/crates/assists/src/handlers/inline_local_variable.rs#L45-L51</a></p>



<a name="216060900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/FindUsages%20help%20with%20local%20pattern/near/216060900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/FindUsages.20help.20with.20local.20pattern.html#216060900">(Nov 09 2020 at 09:39)</a>:</h4>
<p>And yes, there might be some bugs around usages in macros -- don't handle them that well yet :(</p>



<a name="216121754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/FindUsages%20help%20with%20local%20pattern/near/216121754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Arif Roktim <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/FindUsages.20help.20with.20local.20pattern.html#216121754">(Nov 09 2020 at 18:02)</a>:</h4>
<p>I was using <a href="http://inline_local_variable.rs">inline_local_variable.rs</a> as inspiration <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span><br>
You said this is because tests don't have libstd/libcore. Is this the sort of thing FamousDefs tries to solve?</p>



<a name="216130369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/FindUsages%20help%20with%20local%20pattern/near/216130369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/FindUsages.20help.20with.20local.20pattern.html#216130369">(Nov 09 2020 at 19:12)</a>:</h4>
<p>Yep FamousDefs is there to solve this problem ideally by populating it with common things from std/core that tests want to test against</p>



<a name="216130571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/FindUsages%20help%20with%20local%20pattern/near/216130571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/FindUsages.20help.20with.20local.20pattern.html#216130571">(Nov 09 2020 at 19:14)</a>:</h4>
<p><a href="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ide/src/inlay_hints.rs#L441-L442">https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ide/src/inlay_hints.rs#L441-L442</a> here its injected into the inlay_hints tests so that we can test that iterator hint shortening works for example</p>



<a name="216132210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/FindUsages%20help%20with%20local%20pattern/near/216132210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Arif Roktim <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/FindUsages.20help.20with.20local.20pattern.html#216132210">(Nov 09 2020 at 19:27)</a>:</h4>
<p>I see. Thanks all</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>